<?php

namespace Someline\Http\Controllers\Console;

use Illuminate\Http\Request;
use Someline\Component\Wechat\SomelineWechatService;
use Someline\Http\Controllers\BaseController;

class MaterialController extends BaseController
{

    public function getMaterialListForNews(Request $request)
    {

        $app = SomelineWechatService::getWechatApplication();
        $material = $app->material;

        $list_offset = $request->get('offset', 0);
        $list_count = 1;
        $lists = $material->lists('news', $list_offset, $list_count);
        $lists = $lists->toArray(); //console.someline.form

        return view('console.material.material_list', compact('lists', 'list_offset', 'list_count')); //console.material.material_list
    }

    public function getMaterialEditNews($id)
    {
        return view('console.material.news_editor', compact('id'));
    }

    public function getMaterialListForImages(Request $request)
    {

        $app = SomelineWechatService::getWechatApplication();
        $material = $app->material;

        $list_offset = $request->get('offset', 0);
        $list_count = 15;
        $lists = $material->lists('image', $list_offset, $list_count);
        $lists = $lists->toArray();

        return view('console.material.image_list', compact('lists', 'list_offset', 'list_count'));
    }

    public function getFiles(Request $request,$id)
    {

        $list_offset = 0;
        $list_count = 15;
        $lists = null;

        return view('console.material.image_list', compact('lists', 'list_offset', 'list_count'));

        $basePath = "/srv/www/miyingwenhua.com/mitao-audio/storage/app/public/files";
        //http://mitao.miyingwenhua.com/storage/files/ffc39133b829dcd54b26cce15b61662278152ce5.mp3

        $id = 'ffc39133b829dcd54b26cce15b61662278152ce5.mp3';
        $filePath = $basePath . '/' . $id;

    }


    public function getMaterialListForVideos(Request $request)
    {

        $app = SomelineWechatService::getWechatApplication();
        $material = $app->material;

        $list_offset = $request->get('offset', 0);
        $list_count = 15;
        $lists = $material->lists('video', $list_offset, $list_count);
        $lists = $lists->toArray();

        return view('console.material.video_list', compact('lists', 'list_offset', 'list_count'));
    }

    public function getMaterialListForVideosDetail(Request $request, $id)
    {

        $app = SomelineWechatService::getWechatApplication();
        $material = $app->material;

        $result = $material->get($id);

        return redirect($result['down_url']);
    }

    public function getMaterialListForVoices(Request $request)
    {

        $app = SomelineWechatService::getWechatApplication();
        $material = $app->material;

        $list_offset = $request->get('offset', 0);
        $list_count = 15;
        $lists = $material->lists('voice', $list_offset, $list_count);
        $lists = $lists->toArray();

        return view('console.material.voice_list', compact('lists', 'list_offset', 'list_count'));
    }

    public function getMaterialListForVoicesDetail(Request $request, $id)
    {

        $app = SomelineWechatService::getWechatApplication();
        $material = $app->material;

        $result = $material->get($id);
        $result_sha1 = sha1($result);

        $filename = 'voices/' . $result_sha1 . '.mp3';
        \Storage::disk('local')->put($filename, $result);
        $filename = storage_path('app') . '/' . $filename;

        return response()->download($filename, $request->get('name', 'file.mp3'));
    }

}